package com.example.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.appcontact.A004DetailContact;
import com.example.appcontact.R;
import com.example.component.Item003Content;
import com.example.model.Contacts;
import com.example.util.Util;

public class A003DevicesAdapter extends ArrayAdapter<Contacts> {

	private LayoutInflater inflater;
	private List<Contacts> mArr;
	private int layout;
	private OnClickListListener listener;
	private Context context;

	public A003DevicesAdapter(Context context, int textViewResourceId,
			List<Contacts> objects, OnClickListListener listener) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub

		this.layout = textViewResourceId;
		this.mArr = objects;
		this.context = context;
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.listener = listener;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		return getViewPanel(position, convertView);
	}

	@Override
	public Contacts getItem(int position) {
		return super.getItem(position);
	}

	@Override
	public int getCount() {
		return mArr.size();
	}

	class ViewHolder {
		RelativeLayout rl, layout;
		Button btStatus;
		ImageView img_avatar;
		Item003Content content;
	}

	public static interface OnClickListListener {
		public abstract void onClick(Contacts data, int id);
	}

	private View getViewPanel(int position, View convertView) {
		View view = convertView;
		ViewHolder mHolder = null;

		if (view == null) {
			// ??????????????????????????????????????????????????????????????????r???????????????????????????[??????????????????null????????????????????????V?????????????????????????????????????????????r???????????????????????????[??????????????????????????????
			view = inflater.inflate(layout, null);
			mHolder = new ViewHolder();
			mHolder.layout = (RelativeLayout) view
					.findViewById(R.id.item_a003_layout);
			mHolder.btStatus = (Button) view
					.findViewById(R.id.item_a003_iv_img);
			mHolder.img_avatar = (ImageView) view
					.findViewById(R.id.item_a003_iv_avatar);
			mHolder.content = (Item003Content) view
					.findViewById(R.id.item_a003_content);
			mHolder.rl = (RelativeLayout) view
					.findViewById(R.id.item_a003_lr_abc);
			view.setTag(mHolder);
		} else {
			mHolder = (ViewHolder) view.getTag();
		}

		if (mArr.size() > 0) {
			final Contacts contact = mArr.get(position);
			mHolder.content.setName(contact.getFirstName() + " "
					+ contact.getLastName());
			mHolder.content.setPhoneNumber(contact.getPhoneNumber());
			mHolder.content.setEmail(contact.getEmail());
			mHolder.content.setDayBirth(contact.getBirthday());
			mHolder.btStatus.setTag(position);
			try {
				if (mHolder.img_avatar != null && contact.getAvatar() != null) {
					final Bitmap bmp = Util.loadContactPhoto(getContext()
							.getContentResolver(), Long.parseLong(contact
							.getId()));
					if (bmp != null)
						mHolder.img_avatar.setImageBitmap(bmp);
					else
						mHolder.img_avatar
								.setImageResource(R.drawable.personal_default);
				} else {
					mHolder.img_avatar
							.setImageResource(R.drawable.personal_default);
				}
			} catch (Exception e) {
				Log.d("TAG_IMAGE", "Error: " + contact.getAvatar());
				e.printStackTrace();
				mHolder.img_avatar
						.setImageResource(R.drawable.personal_default);
			}

			switch (contact.getStatus()) {
			case 0: // none
				mHolder.btStatus.setVisibility(View.VISIBLE);
				mHolder.layout.setBackgroundColor(context.getResources()
						.getColor(R.color.request_status));
				mHolder.btStatus.setText("Request");
				break;
			case 1: // pending
				mHolder.btStatus.setVisibility(View.VISIBLE);
				mHolder.layout.setBackgroundColor(context.getResources()
						.getColor(R.color.pending_status));
				mHolder.btStatus.setText("Pending");
				break;
			case 3: // accept
				mHolder.layout.setBackgroundColor(context.getResources()
						.getColor(R.color.accpet_status));
				mHolder.btStatus.setVisibility(View.GONE);
				mHolder.btStatus.setText("Accept");
				break;
			case 2: // reject
				mHolder.btStatus.setVisibility(View.VISIBLE);
				mHolder.layout.setBackgroundColor(context.getResources()
						.getColor(R.color.reject_status));
				mHolder.btStatus.setText("Reject");
				break;
			}
			mHolder.layout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (contact != null) {
						Intent inew = new Intent(context,
								A004DetailContact.class);
						inew.putExtra("item", contact);
						context.startActivity(inew);
					}
				}
			});

			mHolder.btStatus.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					int id = Integer.valueOf(v.getTag().toString()).intValue();
					listener.onClick(contact, id);
				}
			});
		}

		return view;
	}
}
